package com.cnblogs.leizia.crm.workbench.service.impl;

import com.cnblogs.leizia.crm.commen.ErrorCode;
import com.cnblogs.leizia.crm.exception.ActivityException;
import com.cnblogs.leizia.crm.utils.DateTimeUtil;
import com.cnblogs.leizia.crm.utils.SqlSessionUtil;
import com.cnblogs.leizia.crm.utils.UUIDUtil;
import com.cnblogs.leizia.crm.vo.PaginationVo;
import com.cnblogs.leizia.crm.workbench.dao.*;
import com.cnblogs.leizia.crm.workbench.domain.*;
import com.cnblogs.leizia.crm.workbench.service.ActivityService;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author leizi
 * @create 2021-02-16 12:42
 */
public class ActivityServiceImpl implements ActivityService {

    /**
     * 添加市场活动信息
     *
     * @param activity
     * @return
     */
    @Override
    public boolean activitySave(Activity activity) {
        ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
        int num = activityDao.activitySave(activity);
        if (num > 0) {
            return true;
        }
        return false;
    }

    /**
     * 分页查询市场活动列表
     *
     * @param activityPageMap 分页查询信息
     * @return
     */
    @Override
    public PaginationVo<Activity> activityPageList(Map<String, Object> activityPageMap) {
        ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
        List<Activity> activities = activityDao.activityPageList(activityPageMap);
        // 查询的是分页后的记录数
//        int size = activities.size();
        // 查询满足条件的总记录数
        int size = activityDao.activityTotal(activityPageMap);

        PaginationVo<Activity> activityPageVo = new PaginationVo<>();
        activityPageVo.setTotal(size);
        activityPageVo.setDatalist(activities);

        return activityPageVo;
    }

    /**
     * 根据ids删除市场活动记录
     * (市场活动表与市场活动备注表关联删除记录)
     *
     * @param idList
     * @return
     */
    @Override
    public boolean activityDelete(String[] idList) {

        ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);

        // 市场活动表与市场活动备注表关联删除记录
        int deleteResult = activityDao.activityDelete(idList);

        if (deleteResult > 0) {
            return true;
        }

        return false;
    }

    /**
     * 根据id获取市场活动记录信息
     *
     * @param id
     * @return
     */
    @Override
    public Activity editActivity(String id) throws ActivityException {
        ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
        Activity activity = activityDao.editActivity(id);

        if (null == activity) {
            throw new ActivityException(ErrorCode.ACTIVITY_EDIT.getMsg());
        }

        return activity;
    }

    @Override
    public boolean updateActivity(Activity activity) throws ActivityException {
        ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
        int updateResult = activityDao.updateActivity(activity);

        if (updateResult > 0) {
            return true;
        } else {
            throw new ActivityException(ErrorCode.ACTIVITY_UPDATE.getMsg());
        }
    }

    /**
     * 根据id获取市场活动详情
     *
     * @param id
     * @return
     */
    @Override
    public Activity getDetail(String id) throws ActivityException {
        ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
        Activity activity = activityDao.getDetail(id);
        if (null == activity) {
            throw new ActivityException(ErrorCode.ACTIVITY_EDIT.getMsg());
        }
        return activity;
    }

    /**
     * 市场活动分页查询列表
     *
     * @param name
     * @param activities
     * @return
     */
    @Override
    public List<Activity> getClueActivityList(String name, List<Activity> activities) {
        ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);

        return activityDao.getClueActivityList(name, activities);
    }

    /**
     * 线索转换
     *
     * @param clueId
     * @param tran
     * @return
     */
    @Override
    public boolean clueConvert(String clueId, Tran tran) {
        ClueDao clueDao = SqlSessionUtil.getSqlSession().getMapper(ClueDao.class);
        ContactsDao contactsDao = SqlSessionUtil.getSqlSession().getMapper(ContactsDao.class);
        CustomerDao customerDao = SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);
        TranDao tranDao = SqlSessionUtil.getSqlSession().getMapper(TranDao.class);

        // 获取线索详情
        Clue clue = clueDao.getClueInfoById(clueId);

        // 获取线索关联的市场活动id
        List<String> activityIds = clueDao.getActivityListById(clueId);

        // 客户id
        String customerId = UUIDUtil.getUUID();
        // 联系人id
        String contactID = UUIDUtil.getUUID();

        // 保存联系人
        Contacts contacts = new Contacts();
        contacts.setOwner(clue.getOwner());
        contacts.setId(contactID);
        contacts.setSource(clue.getSource());
        contacts.setCustomerId(customerId);
        contacts.setFullname(clue.getFullname());
        contacts.setAppellation(clue.getAppellation());
        contacts.setEmail(clue.getEmail());
        contacts.setMphone(clue.getMphone());
        contacts.setJob(clue.getJob());
        contacts.setCreateBy(clue.getCreateBy());
        contacts.setCreateTime(clue.getCreateTime());
        contacts.setDescription(clue.getDescription());
        contacts.setContactSummary(clue.getContactSummary());
        contacts.setNextContactTime(clue.getNextContactTime());
        contacts.setAddress(clue.getAddress());
        contactsDao.saveContacts(contacts);

        // 根据线索id获取线索备注详情
        ClueRemark clueRemark = clueDao.getRemarkByClueId(clueId);

        if (!Objects.isNull(clueRemark)) {
            // 保存联系人备注
            ContactsRemark contactsRemark = new ContactsRemark();
            contactsRemark.setId(UUIDUtil.getUUID());
            contactsRemark.setNoteContent(clueRemark.getNoteContent());
            contactsRemark.setContactsId(contactID);
            contactsRemark.setCreateBy(clueRemark.getCreateBy());
            contactsRemark.setCreateTime(clueRemark.getCreateTime());

        }

        // 保存市场活动与联系人关联表
        ContactsActivityRelation contactsActivityRelation = new ContactsActivityRelation();
        for (String activityId : activityIds) {
            contactsActivityRelation.setId(UUIDUtil.getUUID());
            contactsActivityRelation.setActivityId(activityId);
            contactsActivityRelation.setContactsId(contactID);
            contactsDao.saveActivityContactRelation(contactsActivityRelation);
        }

        // 查询是否已存在客户
        Customer customer = customerDao.getCustomer(clue.getCompany());
        if (Objects.isNull(customer)) {
            // 保存客户
            customer = new Customer();
            customer.setId(customerId);
            customer.setName(clue.getCompany());
            customer.setOwner(clue.getOwner());
            customer.setContactSummary(clue.getContactSummary());
            customer.setNextContactTime(clue.getNextContactTime());
            customer.setWebsite(clue.getWebsite());
            customer.setPhone(clue.getPhone());
            customer.setDescription(clue.getDescription());
            customer.setCreateTime(clue.getCreateTime());
            customer.setCreateBy(clue.getCreateBy());
            customer.setAddress(clue.getAddress());
            customerDao.saveCustomer(customer);
        }

        // 保存客户备注
        if (!Objects.isNull(clueRemark)) {
            CustomerRemark customerRemark = new CustomerRemark();
            customerRemark.setId(UUIDUtil.getUUID());
            customerRemark.setNoteContent(clueRemark.getNoteContent());
            customerRemark.setCreateBy(clueRemark.getCreateBy());
            customerRemark.setCreateTime(clueRemark.getCreateTime());
            customerRemark.setCustomerId(customerId);
            customerDao.saveCustomerRemark(customerRemark);
        }

        // 创建交易
        if (null != tran) {
            tran.setId(UUIDUtil.getUUID());
            tran.setSource(clue.getSource());
            tran.setContactSummary(clue.getContactSummary());
            tran.setNextContactTime(clue.getNextContactTime());
            tran.setCreateTime(DateTimeUtil.getSysTime());
            tran.setCreateBy(clue.getCreateBy());
            tran.setDescription(clue.getDescription());
            // 市场活动id
            tran.setCustomerId(customerId);
            // 联系人id
            tran.setContactsId(contactID);
            tran.setOwner(clue.getOwner());

            // 创建交易历史记录
            TranHistory tranHistory = new TranHistory();
            tranHistory.setId(UUIDUtil.getUUID());
            tranHistory.setCreateBy(tran.getCreateBy());
            tranHistory.setCreateTime(tran.getCreateTime());
            tranHistory.setExpectedDate(tran.getExpectedDate());
            tranHistory.setMoney(tran.getMoney());
            tranHistory.setStage(tran.getStage());
            tranHistory.setTranId(tran.getId());
            tranDao.saveTranHistory(tranHistory);
        }

        // 删除线索记录
        clueDao.deleteClueById(clueId);
        // 删除线索备注
        clueDao.deleteClueRemark(clueId);
        // 删除关联的市场活动
        clueDao.deleteClueRelationActivity(clueId);

        return true;
    }
}
